package view;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.*;
import org.jfree.data.xy.XYSeries;


//import model.Actor;
import model.Counter;
import model.Simulator;


/**
 * Provides an XY line graph with individual lines for every actor.
 * @author Arjan Frans, Alwin Rombout, Jelte Verbree, Brendan Kanters
 * @version 1.0
 */
public class ProgGraph extends AbstractView
{
	private static final long serialVersionUID = 4L;
	private XYSeriesCollection actorData;
    @SuppressWarnings("rawtypes")
    private ArrayList<HashMap<Class, Counter>> historyCache;
    private int progGraphHeight;
    private int progGraphWidth;
    private int total;
    private ChartPanel chartPanel;
	private boolean newchart;
	
    /**
     * Create a new ProgGraph component.
     * @param simulator
     */
    public ProgGraph(Simulator simulator)
    {
    	super(simulator);
    	simulator.addView(this);
    	progGraphHeight = 425;
        progGraphWidth = 700;
        actorData = new XYSeriesCollection();
        historyCache = new ArrayList<HashMap<Class, Counter>>();
        newchart = true;
        
    }


    /**
     * Tell the GUI manager how big we would like to be.
     * @return the new dimension
     */
    public Dimension getPreferredSize()
    {
        return new Dimension(progGraphWidth, progGraphHeight);
    }


    /**
     * Updates the view, clears history and data if step is 1 because this means a reset
     * or new simulation has taken place. If the chart is considered new the actor names are added.
     * If the chart is considered not new all history collected thus far is graphed as a line graph.
     */
    @Override
    public void updateView()
    {
    	HashMap<Class, Counter> newCounters = simulator.getStats().getCounters();
    	total = Integer.parseInt(simulator.getStep());
    	if(total == 1){
    		historyCache.clear();
    		actorData.removeAllSeries();
    		newchart = true;
    		
    	}
    
    	if(newchart == true){
    		for (Class setupKey : newCounters.keySet())
    		{ 
    			actorData.addSeries(new XYSeries(setupKey.getName()));
    		}
    		newchart = false;
    	}
    		
        historyCache.add(newCounters);    
        Iterator<HashMap<Class, Counter>> it = historyCache.iterator();
        while (it.hasNext()) {
        	HashMap<Class, Counter> counters = new HashMap<Class, Counter>();
        	counters = it.next();
        	it.remove();
        	for (Class loopKey : counters.keySet())
        	{
        		Counter count = counters.get(loopKey);
        		actorData.getSeries(count.getName()).add(total, count.getCount());
		   	
        	}
        	
        	
        }
      
        JFreeChart chart = ChartFactory.createXYLineChart("Progression line Chart", "Step", "Count", actorData, PlotOrientation.VERTICAL, true, true, false);
        chartPanel = new ChartPanel(chart);
        this.add(chartPanel);
        
       
      }
       
    	
    }


